package com.sgb.kjwl.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityZulinSearchResultBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.LeaseSearchResultTypeEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseSearchEvent;
import com.sgb.kjwl.view.ui.fragment.home.LeaseSearchCategoryFragment;
import com.sgb.kjwl.viewmodel.home.ZulinSearchResultModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.ShardFragmentPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁搜索结果页面
 */
public class ZulinSearchResultActivity extends AppActivity {
    @Inject
    ZulinSearchResultModel mModel;
    ActivityZulinSearchResultBinding binding;
    private String keyword;

    /**
     * @param keyword
     */
    public static void start(Context context, String keyword) {
        Intent intent = new Intent();
        intent.putExtra("keyword", keyword);
        intent.setClass(context, ZulinSearchResultActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder().appComponent(appComponent).homeModule(new HomeModule(this)).build().inject(this);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ZulinSearchResultActivity.this, R.layout.activity_zulin_search_result);
        binding.ivBack.setOnClickListener(this);
        binding.tvSearch.setOnClickListener(this);
    }

    @Override
    protected void init() {
        keyword = getIntent().getStringExtra("keyword");
        binding.editSearch.setText(keyword);
        mModel.getLeaseType();
    }

    public void updateViews(List<LeaseSearchResultTypeEntity> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            mTitles[i] = data.get(i).getName();
            mFragments.add(LeaseSearchCategoryFragment.newInstance(data.get(i).getCategory(),keyword));
        }
        ShardFragmentPagerAdapter mPagerAdapter = new ShardFragmentPagerAdapter(getSupportFragmentManager(),mFragments, mTitles);
        //SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewPager.setOffscreenPageLimit(mTitles.length);
        binding.viewPager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
        binding.tabLayout.setTabMode(data.size() > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
//                if (TextUtils.isEmpty(binding.editSearch.getText())) {
//                    return;
//                }
                String keyword = binding.editSearch.getText().toString();
                EventBus.getDefault().post(LeaseSearchEvent.make().setMessageTag(EventConstant.EVENT_LEASE_SEARCH_FILTER).setMessageData(keyword));
                recordsLocal(keyword);
                break;
        }
    }

    /**
     * 将搜索词汇记录在本地
     * @param newWords 搜索词
     */
    private void recordsLocal(String newWords) {
        if(TextUtils.isEmpty(newWords)){
            return;
        }
        String historyWords = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.ZULIN_SEARCH_HISTORY, "");
        StringBuffer oldWords = new StringBuffer();
        if (null != historyWords && !historyWords.isEmpty()) {
            String[] arr = historyWords.split(","); //字符串转数组,  用,分割
            for (int i = 0; i < arr.length; i++) {
                if (i < 10) {//最多只记录10条
                    if (newWords.equals(arr[i])) {
                        //剔除相同的字符
                    } else {
                        oldWords.append(arr[i] + ",");
                    }
                }
            }
        }
        String allHistoryWords = newWords + "," + oldWords;
        SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.ZULIN_SEARCH_HISTORY, allHistoryWords);
    }
}
